package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.constants.user.UserConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealNameMapper;
import com.usian.user.service.ApUserRealNameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.hadoop.io.retry.AtMostOnce;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.usian.common.constants.constants.FollowBehaviorConstants.*;

@Service
public class ApUserRealNameServiceImpl extends BaseServiceImpl<ApUserRealNameMapper, ApUserRealname> implements ApUserRealNameService {
    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    @GlobalTransactional
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //参数为空
        if (dto == null) return (PageResponseResult) paramError;
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        Page page = new Page(dto.page, dto.size);
        IPage ipage = page(page, queryWrapper);
        PageResponseResult result = new PageResponseResult(dto.page, dto.size, (int) ipage.getTotal());
        result.setCode(0);
        result.setErrorMessage("查询成功");
        result.setData(ipage.getRecords());
        return result;
    }

    @Override
    public ResponseResult updateStatusById(AuthDto dto) {
        if (dto == null || dto.getId() == null || dto.getStatus() == null || (dto.getStatus() != UserConstants.FAIL_AUTH && dto.getStatus() != UserConstants.PASS_AUTH))
            return paramError;

        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        realname.setStatus(dto.getStatus());
        if (dto.getMsg() != null) realname.setReason(dto.getMsg());
        updateById(realname);
        if (dto.getStatus().equals(UserConstants.PASS_AUTH)) {
            ApUserRealname rzInfo=getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(rzInfo.getUserId());
            WmUser wm = wemediaFeign.findByName(apUser.getName());
            if (wm==null ||wm.getId()==null){
                WmUser wmUser = new WmUser();
                BeanUtils.copyProperties(apUser,wmUser);
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setStatus(UserConstants.PASS_AUTH.intValue());
                wemediaFeign.save(wmUser);

                ApAuthor author = articleFeign.findByUserId(apUser.getId());
                if (author==null){
                    ApAuthor apAuthor = new ApAuthor();
                    apAuthor.setName(apUser.getName());
                    apAuthor.setType(2);
                    apAuthor.setCreatedTime(new Date());
                    apAuthor.setUserId(apUser.getId());
                    WmUser newWmUser = wemediaFeign.findByName(apUser.getName());
                    apAuthor.setWmUserId(newWmUser.getId());
                    articleFeign.save(apAuthor);
                    ApAuthor newApAuthor = articleFeign.findByUserId(apUser.getId());
                    newWmUser.setApAuthorId(newApAuthor.getId());
                }

                apUser.setFlag(Short.valueOf("1"));
                apUserMapper.updateById(apUser);
                kafkaTemplate.send(FOLLOW_BEHAVIOR_TOPIC,"");
            }
            return ResponseResult.okResult(0, "认证通过");
        }
        return ResponseResult.okResult(0, "拒绝通过");

    }


}
